<template>
  <div class="category-management">
    <el-card>
      <template #header>
        <div class="card-header">
          <h2>药品分类管理</h2>
          <el-button type="primary" @click="openCategoryDialog()">新增分类</el-button>
        </div>
      </template>

      <el-table :data="categoryList" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="categoryName" label="分类名称"></el-table-column>
        <el-table-column prop="description" label="分类描述"></el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button type="primary" size="small" @click="openCategoryDialog(scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="deleteCategory(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="handlePageChange"
      ></el-pagination>
    </el-card>

    <!-- 分类编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="isEdit ? '编辑分类' : '新增分类'" width="50%">
      <el-form :model="categoryForm" label-width="100px">
        <el-form-item label="分类名称">
          <el-input v-model="categoryForm.categoryName" placeholder="请输入分类名称"></el-input>
        </el-form-item>

        <el-form-item label="分类描述">
          <el-input v-model="categoryForm.description" type="textarea" placeholder="请输入分类描述"></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveCategory">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { listCategory, getCategory, addCategory, updateCategory, delCategory } from '@/api/category'

// 分类列表
const categoryList = ref([])

// 分页参数
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 对话框显示状态
const dialogVisible = ref(false)

// 是否为编辑模式
const isEdit = ref(false)

// 分类表单
const categoryForm = reactive({
  id: null,
  categoryName: '',
  description: ''
})

// 获取分类列表
const getCategoryList = async () => {
  try {
    const res = await listCategory()
    categoryList.value = res.data || []
    total.value = res.total || 0
  } catch (error) {
    console.error('获取分类列表失败', error)
    ElMessage.error('获取分类列表失败')
  }
}

// 打开分类对话框
const openCategoryDialog = (category) => {
  if (category) {
    // 编辑模式
    isEdit.value = true
    Object.assign(categoryForm, category)
  } else {
    // 新增模式
    isEdit.value = false
    // 重置表单
    Object.assign(categoryForm, {
      id: null,
      categoryName: '',
      description: ''
    })
  }

  dialogVisible.value = true
}

// 保存分类
const saveCategory = async () => {
  try {
    if (isEdit.value) {
      // 编辑分类
      await updateCategory(categoryForm)
      ElMessage.success('分类更新成功')
    } else {
      // 新增分类
      await addCategory(categoryForm)
      ElMessage.success('分类添加成功')
    }

    // 关闭对话框
    dialogVisible.value = false

    // 刷新分类列表
    getCategoryList()
  } catch (error) {
    console.error('保存分类失败', error)
    ElMessage.error('保存分类失败')
  }
}

// 删除分类
const deleteCategory = (category) => {
  ElMessageBox.confirm(`确定要删除分类 ${category.categoryName} 吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      try {
        await delCategory(category.id)
        ElMessage.success('分类删除成功')
        getCategoryList()
      } catch (error) {
        console.error('删除分类失败', error)
        ElMessage.error('删除分类失败')
      }
    })
    .catch(() => {})
}

// 页码变化
const handlePageChange = (page) => {
  currentPage.value = page
  getCategoryList()
}

// 初始化
onMounted(() => {
  getCategoryList()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>
